package com.htwz.webapp

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.tencent.smtt.export.external.interfaces.SslError
import com.tencent.smtt.export.external.interfaces.SslErrorHandler
import com.tencent.smtt.sdk.*
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()

        // 没有加载X5 并且 版本低于8.0 引导用户去手动下载内核
        if (web_view.x5WebViewExtension == null){
            // 去手动下载内核
            web_view.loadUrl("http://debugtbs.qq.com")
            return
        }

        val url = SPUtil.getString(
            this,
            ConfigDialogHelper.KEY_OF_SP_URL,
            ConfigDialogHelper.DEFAULT_URL
        )

//        web_view.setInitialScale(95)

        // 设置按钮
        btn_config.visibility = View.GONE


        if (url.isEmpty()){
            btn_config.performClick()
        }else{
            web_view.loadUrl(url)
//        web_view.loadUrl("https://pre_toilet.schtwz.cn/bigScreen?toiletId=d615aae390904ad6b7ddb8c5a3e65bf7")
//        web_view.loadUrl("http://soft.imtt.qq.com/browser/tes/feedback.html")
        }


        // 配置URL
        btn_config.setOnClickListener {
            ConfigDialogHelper.show(this) {
                if (web_view.url == it) return@show
                Toast.makeText(this, "网页切换中", Toast.LENGTH_SHORT).apply {
                    setGravity(Gravity.CENTER, 0, 0)
                    show()
                }
                web_view.loadUrl(it)
            }
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun init() {

        web_view.webViewClient = object : WebViewClient(){
            override fun onReceivedSslError(p0: WebView?, handler: SslErrorHandler, p2: SslError?) {
                handler.proceed() // 接受所有https证书
            }
        }
        web_view.webChromeClient = WebChromeClient()

        val webSetting: WebSettings = web_view.settings
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
//        webSetting.setSupportZoom(true)
//        webSetting.builtInZoomControls = true
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(false)
        webSetting.setAppCacheEnabled(true)
        webSetting.domStorageEnabled = true
        webSetting.javaScriptEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
        webSetting.setAppCachePath(getDir("appcache", 0).path)
        webSetting.databasePath = getDir("databases", 0).path
        webSetting.setGeolocationDatabasePath(
            getDir("geolocation", 0)
                .path
        )
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            window.decorView.systemUiVisibility =
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        }
    }
}
